package com.xiaomage.slide

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import android.util.AttributeSet
import android.widget.ImageView

/**
 * Created by Administrator on 2018/3/21 0021.
 */
class MyViewLikeImageTest : ImageView {
    //正常图片宽度
    private var imageWidth=0f
    //正常图片高度
    private var imageHeight=0f
    //最大图片宽度
    private var imageWidthMax=0f
    //最大图片高度
    private var imageHeightMax=0f
    //设置斜切后的最大宽度单元片
    private var skewMaxWidth=0f
    //设置斜切后的最大高度单元片
    private var skewMaxHeight=0f
    //图片缩小后的矩阵图
    private var imageNormalMatrix = floatArrayOf()
    //图片放大后的矩阵图
    private var imageChangeBigMatrix = floatArrayOf()


    private var bitmap: Bitmap?=null
    private var myMatrix= Matrix()
    val paint= Paint()
    constructor(context: Context):super(context)
    constructor(context: Context, attributeSet: AttributeSet):super(context,attributeSet)
    fun setImageBitmap(b: Bitmap, imageWidth:Float, imageWidthMax:Float, imageHeight:Float, imageHeightMax:Float) {
        this.imageWidth=imageWidth
        this.imageHeight=imageHeight
        this.imageHeightMax=imageHeightMax
        this.imageWidthMax=imageWidthMax
        skewMaxWidth=(imageWidthMax-imageWidth)/2
        skewMaxHeight=(imageHeightMax-imageHeight)/2
        //算出两种矩阵图
        imageNormalMatrix=floatArrayOf(0f, skewMaxHeight,0f, skewMaxHeight+imageHeight,
                imageWidth, skewMaxHeight+imageHeight,imageWidth, 0f)
        imageChangeBigMatrix=floatArrayOf(0f, 0f,0f, imageHeightMax,
                imageWidthMax, imageHeightMax,imageWidthMax, 0f)
        bitmap = zoomImage(b, imageWidthMax, imageHeightMax)
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if(bitmap!=null){
           /* myMatrix.reset()
            val DX = 100
            val dst  = floatArrayOf(0f+DX, 0f,0f, imageHeightMax,
                    imageWidthMax, imageHeightMax, imageWidthMax-DX, 0f)
            myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);*/
           /* val ch=50f
            val cw=50f
            val dst  = floatArrayOf(0f, skewMaxHeight+ch,0f, skewMaxHeight+imageHeight-ch,
                    imageWidth-cw, skewMaxHeight+imageHeight-ch,imageWidth-cw,skewMaxHeight+ch)
            myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);*/
            //myMatrix.postScale(0.5,0.5f)

            val dx = 80f
            val dy=60f
            ForLog.showError("hahhahahimageWidth-dx:"+(imageWidth-dx))
            val dst  = floatArrayOf(0f, skewMaxHeight+dy,0f, skewMaxHeight+imageHeight-dy,
                    imageWidth-dx, imageHeightMax-dy, imageWidth-dx, dy)
            myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);
            canvas.drawBitmap(bitmap,myMatrix,paint)
        }
    }
    //初始化的时候显示第一个和第三个图片的大小
    fun forImage1(imageHeight:Float,imageHeightMax:Float,imageWidth:Float,imageWidthMax:Float){
        myMatrix.reset()
        val dst  = floatArrayOf(0f, skewMaxHeight,0f, skewMaxHeight+imageHeight,
        imageWidth, skewMaxHeight+imageHeight, imageWidth, skewMaxHeight)
        myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);
        postInvalidate()
    }
    fun set11(number: Float, keyWidth:Float){
        myMatrix.reset()
        //x表示透视图x坐标缩小的长度
        val x=(number*(imageWidthMax-keyWidth))/((imageHeightMax-imageHeight)/2)
        val dst  = floatArrayOf(0f, 0f,0f, imageHeightMax,
                imageWidthMax-x, imageHeightMax-number, imageWidthMax-x, 0f)
        myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);
        postInvalidate()
    }
    fun set12(number: Float,keyWidth:Float){
        myMatrix.reset()
        //x表示透视图x坐标缩小的长度
        val x=(number*(keyWidth-imageWidth))/((imageHeightMax-imageHeight)/2)
        val dst  = floatArrayOf(0f, number,0f, imageHeightMax-2*number,
                keyWidth-x, skewMaxHeight+imageHeight, keyWidth-x,skewMaxHeight)
        myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);
        postInvalidate()
    }
    fun ser31(number: Float,  keyWidth:Float){
        myMatrix.reset()
        //x表示透视图x坐标缩小的长度
        val x=(number*(keyWidth-imageWidth))/((imageHeightMax-imageHeight)/2)
        val dst  = floatArrayOf(0f, skewMaxHeight,0f, skewMaxHeight+imageHeight,
                imageWidth+x, skewMaxHeight-number, imageWidth+x,skewMaxHeight+imageHeight+number)
        myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);
        postInvalidate()
    }
    fun set32(number: Float,keyWidth:Float,xMove:Float){
        myMatrix.reset()
        //x表示透视图x坐标缩小的长度
        val dst  = floatArrayOf(0f, skewMaxHeight-number,0f, skewMaxHeight+imageHeight+number,
                keyWidth+xMove, imageHeightMax,keyWidth+xMove,0f)
        myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);
        postInvalidate()
    }
    //------------------------------------------------------------------------------
    fun set12_2(number: Float,imageHeight:Float,imageHeightMax:Float,imageWidth:Float,imageWidthMax:Float,keyWidth:Float,xMove:Float){
        myMatrix.reset()
        //x表示透视图x坐标缩小的长度
        val dst  = floatArrayOf(0f, number,0f, imageHeightMax-number,
                keyWidth+xMove, skewMaxHeight+imageHeight,keyWidth+xMove,skewMaxHeight)
        myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);
        postInvalidate()
    }
    fun ser31_2(number: Float, imageHeight:Float, imageHeightMax:Float, imageWidth:Float, imageWidthMax:Float, keyWidth:Float,xMove:Float){
        myMatrix.reset()
        //x表示透视图x坐标缩小的长度
        val dst  = floatArrayOf(0f, skewMaxHeight,0f, skewMaxHeight+imageHeight,
                imageWidth-xMove, skewMaxHeight+imageHeight+number,imageWidth-xMove,skewMaxHeight-number)
        myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);
        postInvalidate()
    }
    //------------------------------------第二阶段3d操作------------------------------------------
    fun stateTwoChangeSmall(cw:Float,ch:Float){
        myMatrix.reset()
        //x表示透视图x坐标缩小的长度
        val dst  = floatArrayOf(0f, skewMaxHeight+ch,0f, skewMaxHeight+imageHeight-ch,
                imageWidth-cw, skewMaxHeight+imageHeight-ch,imageWidth-cw,skewMaxHeight+ch)
        myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);
        postInvalidate()
    }
    fun stateTwoChangeBig(cw:Float,ch:Float,skewMaxHeight:Float,imageHeight:Float,stateTwoMixWidth:Float,stateTwoMixHeight:Float){
        myMatrix.reset()
        //x表示透视图x坐标缩小的长度
        val dst  = floatArrayOf(0f, (imageHeight-stateTwoMixHeight)/2-ch+skewMaxHeight,0f, (imageHeight-stateTwoMixHeight)/2-ch+skewMaxHeight+ch*2+stateTwoMixHeight,
                stateTwoMixWidth+cw, (imageHeight-stateTwoMixHeight)/2-ch+skewMaxHeight+ch*2+stateTwoMixHeight,stateTwoMixWidth+cw,(imageHeight-stateTwoMixHeight)/2-ch+skewMaxHeight)
        myMatrix.setPolyToPoly(imageChangeBigMatrix, 0, dst, 0, 4);
        postInvalidate()
    }

    fun zoomImage(bgimage: Bitmap, newWidth: Float,
                  newHeight: Float): Bitmap {
        // 获取这个图片的宽和高
        val width = bgimage.width.toFloat()
        val height = bgimage.height.toFloat()
        // 创建操作图片用的matrix对象
        val myMatrix = Matrix()
        // 计算宽高缩放率
        val scaleWidth = newWidth.toFloat() / width
        val scaleHeight = newHeight.toFloat() / height
        // 缩放图片动作
        myMatrix.postScale(scaleWidth, scaleHeight)
        return Bitmap.createBitmap(bgimage, 0, 0, width.toInt(),
                height.toInt(), myMatrix, true)
    }

}